const express = require("express");
const cors = require("cors");
const app = express();

app.use(
  cors({
    origin: "http://127.0.0.1:5500", // 允许的源
    methods: ["GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS"],
    allowedHeaders: ["school"], // 允许的自定义头
    exposedHeaders: ["abc"],
  })
);
// app.use(cors());

const students = [
  { id: 1, name: "张三", age: 18 },
  { id: 2, name: "李四", age: 19 },
  { id: 3, name: "王五", age: 20 },
];

app.get("/students", (req, res) => {
  // //   res.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:5500");
  // //   res.setHeader("Access-Control-Allow-Origin", "http://localhost:5500");
  // res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader("abc", "xyz");
  res.send(students);
});
// app.options("/students", (req, res) => {
//   res.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:5500");
//   res.setHeader("Access-Control-Allow-Method", "GET");
//   res.setHeader("Access-Control-Allow-Headers", "school,Content-Type");
//   // res.setHeader("Access-Control-Allow-Headers", "*");
//   res.setHeader("Access-Control-Max-Age", 70);
//   res.send();
// });

app.listen(8081, () => {
  console.log("服务器启动成功");
});
